<?php
/**
 * @version $Id: ObjectMother.php 417 2009-05-29 14:00:00Z Alexander.Anokhin $
 * @author Alexander Anokhin
 * @package Kpl
 */

set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());

require_once ('ConnManager.php');

class Kpl_ObjectMother
{
	private $zdb;

	private $alreadyCreatedTables = array();

	public function initDb() {

		if($this->zdb) return $this->zdb;

		$connManager = new Kpl_Action_Helper_ConnManager();

		$zdb = $connManager->getConnection();

		/**
		 * Test data environment generated
		 */

		$this->tables['Menu'] = 'm01';
		$ddl['Menu'] = "CREATE TABLE m01 (
            'id'        INTEGER,
            'ga'        CHAR(1) NOT NULL,
            'dir'       CHAR(24),
            'remark'    CHAR(40),
            'sort'      CHAR(1),
            'name'      CHAR(24),
            'argv'      VARCHAR(10),
            'param'     INTEGER,
            'type'      VARCHAR(1),
			'object'	VARCHAR(40),
			'mask'    	VARCHAR(16)
        )";

		$this->tables['Users'] = 'm40';
		$ddl['Users'] = "CREATE TABLE m40 (
            'post_nom' INTEGER PRIMARY KEY,
            'passwd'   CHAR(12),
            'groupmem' CHAR(32),
            'rolemem'  CHAR(32),
            'id_bit'   BYTE
        )";

		$this->tables['Employees'] = 'm03';
		$ddl['Employees'] = "CREATE TABLE m03 (
             'post_nom'		INTEGER
             ,'tab_nom' 	CHAR(8)
             ,'famili'  	CHAR(15)
             ,'name1'		CHAR(15)
             ,'name2'		CHAR(15)
             ,'prof_dolg'	INTEGER	
             ,'sost'		CHAR(1)
             ,'data_rogd'	DATE
		)";

		$this->tables['Groups'] = 'm41';
		$ddl['Groups'] = "
        	CREATE TABLE m41 (
        		'access' CHAR(512),
        		'groupname' CHAR(32) PRIMARY KEY,
        		'dirstart' CHAR(32),
        		'id' VARCHAR(50)
        	)
        ";

		$this->tables['Persons'] = 'person';
		$ddl['Persons'] = "
        	CREATE TABLE person (
        		'person_id'	INTEGER,
        		'last_name' VARCHAR(25),
        		'first_name' VARCHAR(20),
        		'midle_name' VARCHAR(20),
        		'birth_date' DATE,
        		'death_date' DATE,
        		'sex'	INTEGER,
        		'id_ras' INTEGER,
        		'prsn_csv_id'	INTEGER,
        		'start' INTEGER,
        		'stop_' INTEGER
        	)
        ";

		$this->tables['PersonDocs'] = 'person_doc';
		$ddl['PersonDocs'] = "
        	CREATE TABLE person_doc (
        		'person_id' INTEGER,
        		'doc_id' INTEGER,
        		'start' INTEGER,
        		'stop_' INTEGER,
        		'type' INTEGER
        	)
        ";

		$this->tables['Documents'] = 'docum';
		$ddl['Documents'] = "
        	CREATE TABLE docum (
        		'doc_id' INTEGER PRIMARY KEY AUTOINCREMENT,
        		'type' INTEGER,
        		'd_pol' DATE,
        		'id_np' INTEGER,
        		'id_dist' INTEGER,
        		'id_er' INTEGER,
        		'id_iso' INTEGER,
        		'id_com' INTEGER,
        		'name' VARCHAR(100)
        	)
        ";

		$this->tables['Requisites'] = 'rekvizit';
		$ddl['Requisites'] = "
        	CREATE TABLE rekvizit (
        		'type_doc'	INTEGER,
        		'id_rek'	INTEGER
        	)
        ";

		$this->tables['RequisiteTypes'] = 'zc_rekvizit_id_rek';
		$ddl['RequisiteTypes'] = "
        	CREATE TABLE zc_rekvizit_id_rek (
        		'id_rek' INTEGER,
        		'descr'	 VARCHAR(21)
        	)
        ";

		$this->tables['DocumentTypes'] = 'zc_docum_type';
		$ddl['DocumentTypes'] = "
        	CREATE TABLE zc_docum_type (
        		'type'		INTEGER,
        		'descr'		VARCHAR(70),
        		'docupd'	VARCHAR(50),
        		'docsel' 	VARCHAR(50)
        	)
        ";

		$this->tables['ReqValues'] = 'doc_rek';
		$ddl['ReqValues'] = "
        	CREATE TABLE doc_rek (
        		'doc_id'		INTEGER,
        		'id_rek'		INTEGER,
        		'value'			VARCHAR(50)
        	)
        ";
		
		$this->tables['Street'] = 'street';
		$ddl['Street'] = "
        	CREATE TABLE street (
        		'id' INTEGER,
        		'name' VARCHAR(32),
        		'type' INTEGER
        	)
        ";
		
		$this->tables['BuildStr'] = 'build_str';
		$ddl['BuildStr'] = "
        	CREATE TABLE build_str (
				'build_code' INTEGER,
				'build_n' VARCHAR(5)
        	)
        ";

		$this->tables['Accounts'] = 'account';
        $ddl['Accounts'] = "
        	CREATE TABLE account (
			        'acc_n'  INTEGER,
			        'build_code'  INTEGER,
			        'flat' INTEGER,
			        'room_n'  INTEGER,
			        'room_num' INTEGER,
			        'sq_total'  REAL,
			        'sq_liv'  REAL,
			        'pmnt_form'   INTEGER,
			        'type_tarif'   INTEGER,
			        'floor'   INTEGER,
			        'comment'  VARCHAR(32),
			        'keyplot'  INTEGER,
			        'acc_key'   INTEGER
        	)
        ";
        
	   $this->tables['BillHdrst'] = 'bill_hdr';
       $ddl['BillHdrst'] =  "CREATE TABLE bill_hdr (
	        	'acc_n'  INTEGER,
	        	'idate' INTEGER,
	        	'amm_start' REAL,
	        	'amm_end' REAL,
	        	'sq_x' REAL,
	        	'sq_d' REAL,
	        	'dolg' REAL,
	        	'time_dolg' INTEGER,
	        	'peny_end' REAL,
	        	'ben_amm' REAL,
	        	'organiz_id' INTEGER,
	        	'benefs' VARCHAR(16),
	        	'ben_num' INTEGER,
	        	'comp_dolg' REAL,
	        	'comp_amm' REAL,
	        	'receip' REAL,
	        	'peny_receip' REAL,
	        	'person_id' INTEGER,
	        	'type_calc' INTEGER,
	        	'compens' REAL,
	        	'inh_num' REAL,
	        	'personijd' REAL,
	        	'personinh' REAL,
	        	'personown' REAL,
	        	'personout' REAL
        	)
        ";
       
	   $this->tables['Setval'] = 'setval';
        $ddl['Setval'] = "
        	CREATE TABLE setval (
			     'start' INTEGER,
			     'stop_' INTEGER,
			     'name' varchar(16),
			     'val' varchar(512)
        	)
        ";
        
	   $this->tables['Organiz'] = 'organiz';
        $ddl['Organiz'] = "
        	CREATE TABLE organiz (
				'id' INTEGER,
				'id_up' INTEGER,
				'name' varchar(10),
				'type' INTEGER,
				'id_bank' INTEGER,
				'idate' INTEGER,
				'bu_idate' INTEGER,
				'pc_idate' INTEGER,
				'quart' varchar(32),
				'part' INTEGER,
				'id_seq' INTEGER ,
				'id_bit' varchar(3),
				'reclamma' INTEGER
        	)
        ";
        
	   $this->tables['PersonAcc'] = 'person_acc';
        $ddl['PersonAcc'] = "CREATE TABLE person_acc (
        	'start' INTEGER,
        	'stop_' INTEGER,
        	'type' INTEGER,
        	'person_id' INTEGER,
        	'acc_n' INTEGER,
        	'dp_zvl' INTEGER,
        	'dv_zvl' INTEGER,
        	'pvs' INTEGER,
        	'idate' INTEGER,
        	'reg' INTEGER,
        	'prim' varchar(100),
        	'build_code' INTEGER,
        	'flat' INTEGER,
        	'room_n' INTEGER,
        	'pk_id' INTEGER,
        	'acc_key' INTEGER
        )";

		$this->tables['fakeSuggestions'] = 'fake_suggestions';
		$ddl['fakeSuggestions'] = "CREATE TABLE fake_suggestions (
				'name' VARCHAR(10),
				'code' INTEGER PRIMARY KEY AUTOINCREMENT,
				'filter1' INTEGER,
				'filter2' VARCHAR(10)
				)";




        foreach($ddl as $stmt)
        $zdb->query($stmt);

        $this->zdb = $zdb;
        Zend_Db_Table::setDefaultAdapter($this->zdb);

        $this->alreadyCreatedTables = array();

        return $this->zdb;
	}

	public function cleanTestData($modelname) {
		$this->zdb->query('DROP TABLE IF EXISTS ' . $this->tables[$modelname]);
		unset($this->alreadyCreatedTables[$modelname]);
	}

	/**
	 * @param Zend_Db_Adapter_Abstract $zdb
	 */
	public function fillWithTestDataFor($modelname, $dump_postfix = '')
	{
		if (in_array($modelname,$this->alreadyCreatedTables)) return $this;

		if(!$this->zdb)
		$this->initDb();

		$testDataFilename = dirname(__FILE__) . '/dumps/' . $modelname . $dump_postfix . '.php';
			
		if(file_exists($testDataFilename))
		{
			include($testDataFilename);
		}
		else
		{
			throw new Exception('No test data file founded [' . $testDataFilename . ']');
		}

		$testDataInsertsArrayName = $modelname . '_dump';

		if(!is_array($$testDataInsertsArrayName))
		throw new Exception('Array [' . $testDataInsertsArrayName . '] is needed.');

		foreach($$testDataInsertsArrayName as $insertSql) {
			$this->zdb->query($insertSql);
				
		}

		$this->alreadyCreatedTables[$modelname] = $modelname;

		return $this;
	}

	public function getFake($fakeName, $opts = null)
	{
		$fakeClassName = 'fakes_'  . $fakeName;
		return new $fakeClassName($opts);
	}
}
